﻿Public Class QuickBoardViewModel
  Inherits BaseViewModel

  Private _sprintModel As SprintModel

  Private _refreshCommand As RelayCommand
  Private _optionsCommand As RelayCommand

  Private WithEvents _UpdateTimer As New System.Windows.Threading.DispatcherTimer()

  Private _ElapsedSeconds As Integer = 0

  Public Sub New()
    _sprintModel = New SprintModel(My.Settings.TFSServerName, My.Settings.TFSReportServerURL, _
                                   My.Settings.TFSReportURL, My.Settings.TeamProjectName, My.Settings.IterationPath)

    If My.Settings.AutoRefresh Then
      _UpdateTimer.Interval = New TimeSpan(0, 0, 1)
      _UpdateTimer.Start()
    End If

  End Sub

#Region "View Properties"

  Public ReadOnly Property WindowTitle() As String
    Get
      Dim assembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
      Return "Scrum for Team System QuickBoard (" & VersionModel.AssemblyVersion & " - " & VersionModel.AssemblyLastWriteDate.ToString("MM/dd/yyyy") & ")"
    End Get

  End Property

  Public ReadOnly Property ReportImage() As BitmapImage
    Get
      Return _sprintModel.ReportImage
    End Get
  End Property

  Public ReadOnly Property SprintName() As String
    Get
      Return _sprintModel.SprintName
    End Get
  End Property

  Public ReadOnly Property BugCount() As Integer
    Get
      Return _sprintModel.BugCount
    End Get
  End Property

  Public ReadOnly Property PBICount() As Integer
    Get
      Return _sprintModel.PBICount
    End Get
  End Property

  Public ReadOnly Property SBICount() As Integer
    Get
      Return _sprintModel.SBICount
    End Get
  End Property

  Public ReadOnly Property ImpedimentCount() As Integer
    Get
      Return _sprintModel.ImpedimentCount
    End Get
  End Property

  Public ReadOnly Property DaysRemainingInSprint() As Integer
    Get
      If _sprintModel.DaysRemainingInSprint = -999 Then
        Return Nothing
      Else
        Return CInt(_sprintModel.DaysRemainingInSprint)
      End If
    End Get
  End Property

  Public ReadOnly Property Users() As Generic.List(Of UserModel)
    Get
      Return _sprintModel.Users.Values.ToList
    End Get
  End Property

  Public ReadOnly Property PBINotDone() As SprintModel.SummaryData
    Get
      Return _sprintModel.PBISummary("Not Done")
    End Get
  End Property

  Public ReadOnly Property PBITest() As SprintModel.SummaryData
    Get
      Return _sprintModel.PBISummary("Ready For Test")
    End Get
  End Property

  Public ReadOnly Property PBIInProgress() As SprintModel.SummaryData
    Get
      Return _sprintModel.PBISummary("In Progress")
    End Get
  End Property

  Public ReadOnly Property PBIDone() As SprintModel.SummaryData
    Get
      Return _sprintModel.PBISummary("Done")
    End Get
  End Property

  Public ReadOnly Property SBINotDone() As SprintModel.SummaryData
    Get
      Return _sprintModel.SBISummary("Not Done")
    End Get
  End Property

  Public ReadOnly Property SBIInProgress() As SprintModel.SummaryData
    Get
      Return _sprintModel.SBISummary("In Progress")
    End Get
  End Property

  Public ReadOnly Property SBITest() As SprintModel.SummaryData
    Get
      Return _sprintModel.SBISummary("Ready for Test")
    End Get
  End Property

  Public ReadOnly Property SBIDone() As SprintModel.SummaryData
    Get
      Return _sprintModel.SBISummary("Done")
    End Get
  End Property

  Public ReadOnly Property UpdateCountdown() As String
    Get
      Return TimeHelpers.ConvertSecondstoCountdownTime(My.Settings.RefreshIntervalSeconds - _ElapsedSeconds)
    End Get
  End Property

#End Region

#Region "Commands"

  Public ReadOnly Property OptionsCommand() As ICommand
    Get
      If _optionsCommand Is Nothing Then
        Dim requestOptionAction As New Action(Of Object)(AddressOf OnOptions)
        _optionsCommand = New RelayCommand(requestOptionAction)
      End If

      Return _optionsCommand
    End Get
  End Property

  Public ReadOnly Property RefreshCommand() As ICommand
    Get
      If _refreshCommand Is Nothing Then
        Dim requestRefreshAction As New Action(Of Object)(AddressOf OnRefreshData)
        _refreshCommand = New RelayCommand(requestRefreshAction)
      End If

      Return _refreshCommand
    End Get
  End Property

#End Region

#Region "Command and Event Handlers"

  Private Sub OnRefreshData(ByVal s As Object)
    _sprintModel.RefreshData()
    MyBase.OnPropertyChanged("ReportImage")
    MyBase.OnPropertyChanged("SprintName")
    MyBase.OnPropertyChanged("BugCount")
    MyBase.OnPropertyChanged("PBICount")
    MyBase.OnPropertyChanged("SBICount")
    MyBase.OnPropertyChanged("ImpedimentCount")
    MyBase.OnPropertyChanged("DaysRemainingInSprint")
    MyBase.OnPropertyChanged("Users")

    MyBase.OnPropertyChanged("PBINotDone")
    MyBase.OnPropertyChanged("PBITest")
    MyBase.OnPropertyChanged("PBIInProgress")
    MyBase.OnPropertyChanged("PBIDone")

    MyBase.OnPropertyChanged("SBINotDone")
    MyBase.OnPropertyChanged("SBITest")
    MyBase.OnPropertyChanged("SBIInProgress")
    MyBase.OnPropertyChanged("SBIDone")

    MyBase.OnPropertyChanged("UpdateCountdown")

  End Sub

  Private Sub OnOptions(ByVal e As Object)
    Dim opVM As New OptionsViewModel

    opVM.ShowCommand.Execute(Me)

    If opVM.OptionsChanged Then
      _sprintModel = New SprintModel(My.Settings.TFSServerName, My.Settings.TFSReportServerURL, _
                               My.Settings.TFSReportURL, My.Settings.TeamProjectName, My.Settings.IterationPath)

      RefreshCommand.Execute(Nothing)

      If My.Settings.AutoRefresh Then
        _UpdateTimer.Interval = New TimeSpan(0, 0, 1)
        _UpdateTimer.Start()
      Else
        _UpdateTimer.Stop()
      End If

    End If

  End Sub

  Private Sub _UpdateTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles _UpdateTimer.Tick
    If My.Settings.AutoRefresh = False Then
      _UpdateTimer.Stop()
    End If

    _ElapsedSeconds += 1
    If _ElapsedSeconds >= My.Settings.RefreshIntervalSeconds Then
      _UpdateTimer.Stop()
      _ElapsedSeconds = 0
      MyBase.OnPropertyChanged("UpdateCountdown")
      _refreshCommand.Execute(Nothing)
      _UpdateTimer.Start()
    Else
      'update time label
      MyBase.OnPropertyChanged("UpdateCountdown")
    End If

  End Sub

#End Region

End Class
